package com.leyou.item.service;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dao.SpecGroupMapper;
import com.leyou.item.dao.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author:yanjizhw@163.com
 * @date:2019-10-03 12:33
 * 描述:
 * @since:1.0.0
 */
@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> querySpecGroup(SpecGroup specGroup){

        List<SpecGroup> groups = specGroupMapper.select(specGroup);

        if (CollectionUtils.isEmpty(groups)){
            throw new LyException(ExceptionEnum.SPEC_GROUP_NOT_FOUND);
        }

        return groups;
    }

    public List<SpecParam> querySpecParam(SpecParam specParam) {

        List<SpecParam> params = specParamMapper.select(specParam);

        if (CollectionUtils.isEmpty(params)){
            throw new LyException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }

        return params;
    }

    public void addGroup(SpecGroup specGroup) {

        specGroup.setId(null);

        int count = specGroupMapper.insert(specGroup);

        if (count != 1){
            throw new LyException(ExceptionEnum.SPEC_GROUP_CREATE_FAILED);
        }
    }

    public void modifyGroup(SpecGroup specGroup) {

        int update = specGroupMapper.updateByPrimaryKeySelective(specGroup);

        if (update !=1){
            throw new LyException(ExceptionEnum.UPDATE_SPEC_GROUP_FAILED);
        }
    }

    public void moveSpecGroup(SpecGroup specGroup) {

        int delete = specGroupMapper.deleteByPrimaryKey(specGroup);

        if (delete != 1){
            throw new LyException(ExceptionEnum.DELETE_SPEC_GROUP_FAILED);
        }
    }

    public void addParam(SpecParam specParam) {

        specParam.setId(null);

        int insert = specParamMapper.insert(specParam);

        if (insert != 1){
            throw new LyException(ExceptionEnum.SPEC_PARAM_CREATE_FAILED);
        }
    }
}
